#include "brezenhem.h"
#include "QDebug"

Brezenhem::Brezenhem()
{
}

PointsIter* Brezenhem::calculate(QPoint point){
    mPoints->clear();
    if ((*mFirstPoint) == point){
        mPoints->append(*mFirstPoint);
        return new PointsIter(*mPoints);
    }

    QPoint delta = point - *mFirstPoint;
    int dx = delta.x();
    int dy = delta.y();
    int incX = dx < 0 ? -1 : 1;
    int incY = dy < 0 ? -1 : 1;
    dx = qAbs(-dx);
    dy = qAbs(-dy);
    int pdx, pdy, es, el;
    if(dx > dy){
        pdx =  incX;
        pdy = 0;
        es = dy;
        el = dx;
    } else {
        pdx = 0;
        pdy = incY;
        es = dx;
        el = dy;
    }
    int x = mFirstPoint->x();
    int y = mFirstPoint->y();
    int err = el/2;
    mPoints->append(*mFirstPoint);
    for(int i = 0; i < el; i++){
        err -= es;
        if(err < 0){
            err += el;
            x += incX;
            y += incY;
        } else {
            x += pdx;
            y += pdy;
        }
        mPoints->append(QPoint(x, y));
    }
    return new PointsIter(*mPoints);
}
